//
//  RegistViewController.m
//  Travel
//
//  Created by lanou3g on 15/12/16.
//  Copyright © 2015年 涂晓雨. All rights reserved.
//

#import "RegistViewController.h"
#import "UserModel.h"
#import "DataBaseTool.h"
@interface RegistViewController ()
@property(nonatomic,strong)UserModel *model;
@end

@implementation RegistViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
       self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"注册.jpeg"]];
    
    self.userName = [[UITextField alloc]initWithFrame:CGRectMake(30, 80, self.view.frame.size.width - 60, 30)];
    self.userName.placeholder = @"请输入用户名";
    self.userName.borderStyle = UITextBorderStyleRoundedRect;
    
    [self.view addSubview:self.userName];
    
    
    self.pass = [[UITextField alloc]initWithFrame:CGRectMake(30, 130, self.view.frame.size.width - 60, 30)];
    self.pass.placeholder = @"请输入密码";
    self.pass.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.pass];
    
    
    self.repeadword = [[UITextField alloc]initWithFrame:CGRectMake(30, 180, self.view.frame.size.width - 60, 30)];
    self.repeadword.placeholder = @"确认密码";
    self.repeadword.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.repeadword];

    [self registButton:CGRectMake((self.view.frame.size.width/2) - 50, 230, 100, 30) title:@"确定" action:@selector(action)];
}

-(void)action{
    
    self.model = [[UserModel alloc]init];
    NSArray *array = [[DataBaseTool shareDataBaseTool]selectAllUser];
    self.model.name = self.userName.text;
    self.model.password = self.pass.text;
 
    for (UserModel *model in array) {
        if (model.name == self.userName.text) {
            UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:@"该用户已经注册过了" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alter animated:YES completion:nil];
            UIAlertAction *alterAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [alter addAction:alterAction];
            return;
        }
    }
  
    if (self.userName.text.length >0 && self.pass.text.length >0) {
          if ([self.repeadword.text isEqualToString:self.pass.text]) {
               [[DataBaseTool shareDataBaseTool]insertWithModel:self.model];
               [self dismissViewControllerAnimated:YES completion:nil];
          }else{
              UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次密码输入不正确" preferredStyle:UIAlertControllerStyleAlert];
              [self presentViewController:alter animated:YES completion:nil];
              UIAlertAction *alterAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
              [alter addAction:alterAction];

          
          }
    }else{
    
        UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入用户名" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alter animated:YES completion:nil];
        UIAlertAction *alterAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alter addAction:alterAction];
    
    }


}

//注册的点击框
-(void)registButton:(CGRect)frame  title:(NSString *)str action:(SEL)action{
    UIButton *button = [[UIButton alloc]initWithFrame:frame];
    
    button.layer.masksToBounds = YES;
    
    button.layer.cornerRadius = 10;
    
    button.backgroundColor = [UIColor brownColor];
    
    [button setTitle:str forState:UIControlStateNormal];
    
    [self.view addSubview:button];
    
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
}
@end
